{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 向量或矩阵中的所有元素求和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "# 使用numpy创建100个随机数\n",
    "L = np.random.random(100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.43331983e-01, 6.14367745e-01, 3.06028305e-02, 9.41866565e-01,\n",
       "       9.26302561e-01, 3.93010350e-04, 9.08389304e-01, 4.55477641e-01,\n",
       "       6.95405832e-03, 3.32039622e-01, 7.07171791e-01, 2.37681968e-01,\n",
       "       8.44554949e-01, 7.90238262e-01, 9.96683198e-01, 4.37126151e-01,\n",
       "       7.77163198e-01, 7.40800032e-01, 1.07719465e-01, 2.90109681e-01,\n",
       "       1.86644943e-01, 2.15170111e-02, 5.04673777e-01, 7.93706516e-01,\n",
       "       6.00517547e-01, 4.76798962e-01, 4.36188016e-01, 3.95632959e-01,\n",
       "       8.46549947e-01, 9.39900796e-01, 3.34869418e-01, 2.17262265e-02,\n",
       "       4.25106442e-01, 9.55303760e-01, 6.04686651e-01, 9.55452325e-01,\n",
       "       2.98941479e-01, 6.79384952e-01, 8.27947315e-01, 5.14748046e-02,\n",
       "       4.89766807e-01, 4.48026825e-01, 7.15146428e-01, 9.78241945e-01,\n",
       "       7.53623363e-01, 5.34737536e-01, 3.84457345e-01, 4.68753380e-01,\n",
       "       4.36549356e-01, 2.05084742e-01, 8.19090362e-02, 5.74190143e-01,\n",
       "       1.17855691e-01, 8.83324261e-01, 6.35455100e-01, 9.76032609e-01,\n",
       "       7.40344712e-02, 1.65015536e-01, 4.44620556e-01, 5.84337028e-01,\n",
       "       6.41237471e-01, 7.32514343e-01, 1.08834871e-01, 6.52341259e-01,\n",
       "       3.67645489e-01, 9.38034583e-01, 7.33030245e-01, 7.80305152e-01,\n",
       "       7.53534861e-01, 8.84268218e-01, 5.43117848e-01, 3.07908933e-01,\n",
       "       1.90367209e-01, 7.89861475e-01, 9.38823046e-02, 2.47251650e-01,\n",
       "       1.19506752e-03, 9.14071817e-01, 8.85924876e-02, 6.86184911e-01,\n",
       "       9.57640029e-01, 1.58323354e-01, 1.45939520e-02, 3.90803447e-01,\n",
       "       7.40437148e-02, 8.49582308e-01, 2.39761186e-01, 2.75759226e-01,\n",
       "       6.60704261e-01, 7.53181592e-01, 4.31476392e-01, 2.25700163e-01,\n",
       "       5.84008090e-01, 2.91237215e-01, 2.01539094e-01, 6.54139966e-01,\n",
       "       8.14810358e-01, 3.71546907e-01, 2.82915079e-01, 7.57142183e-01])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "50.04024873286174"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 调用Python自带的sum方法，对向量L中的100个随机数进行求和\n",
    "sum(L)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "50.04024873286173"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 调用numpy中的sum方法，对向量L中的100个随机数进行求和\n",
    "np.sum(L)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "106 ms ± 3.63 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n",
      "1.18 ms ± 59.8 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
     ]
    }
   ],
   "source": [
    "# 生成一百万个随机数进行运算效率测试\n",
    "big_array = np.random.rand(1000000)\n",
    "# Python自带的求和方法\n",
    "%timeit sum(big_array)\n",
    "# numpy中的求和方法\n",
    "%timeit np.sum(big_array)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<font color=red>numpy的求和方法的性能比Python自带的求和方法快的多</font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11],\n",
       "       [12, 13, 14, 15]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.arange(16).reshape(4, -1)\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "120"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求矩阵A所有元素的和\n",
    "np.sum(A)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 向量或矩阵中所有元素求最大值和最小值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.87332142, 0.19441355, 0.812485  , 0.04835741, 0.54775318,\n",
       "       0.01029016, 0.0286549 , 0.89897891, 0.06555574, 0.76844514,\n",
       "       0.21702946, 0.8254805 , 0.67471337, 0.39416822, 0.17463292,\n",
       "       0.17366917, 0.49009893, 0.47653671, 0.36735381, 0.19531617,\n",
       "       0.90088697, 0.25413874, 0.84578024, 0.39645387, 0.89382452,\n",
       "       0.73284035, 0.82206454, 0.10739889, 0.00728745, 0.1769413 ,\n",
       "       0.68689181, 0.25467097, 0.97089974, 0.14183423, 0.13995727,\n",
       "       0.82820103, 0.18115647, 0.92329826, 0.65543785, 0.98773029,\n",
       "       0.93060036, 0.4923053 , 0.4661371 , 0.61559202, 0.33183829,\n",
       "       0.37726854, 0.51768546, 0.65003456, 0.08015991, 0.53231767,\n",
       "       0.75927958, 0.69805546, 0.45222345, 0.65429597, 0.14539713,\n",
       "       0.24155268, 0.8822685 , 0.02226534, 0.91702204, 0.86266067,\n",
       "       0.66579879, 0.92440572, 0.49366681, 0.34167085, 0.90837908,\n",
       "       0.87322435, 0.14740578, 0.4349946 , 0.08667473, 0.0215263 ,\n",
       "       0.03238157, 0.82008229, 0.10726898, 0.87369549, 0.73091489,\n",
       "       0.98234389, 0.44747295, 0.19612985, 0.47523545, 0.11334776,\n",
       "       0.08220939, 0.95592797, 0.02040241, 0.6657025 , 0.13980807,\n",
       "       0.48266726, 0.76576263, 0.8303729 , 0.70306815, 0.7389254 ,\n",
       "       0.18345709, 0.92866035, 0.95115671, 0.89865363, 0.55175856,\n",
       "       0.06847951, 0.47380579, 0.2335756 , 0.55804172, 0.45390935])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.rand(100)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.007287449773870969"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求最小值的写法1：\n",
    "np.min(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.007287449773870969"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求最小值的写法2：\n",
    "a.min()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9877302945331379"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求最大值的写法1：\n",
    "np.max(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9877302945331379"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求最大值的写法2：\n",
    "a.max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11],\n",
       "       [12, 13, 14, 15]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.arange(16).reshape(4, -1)\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "15"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求矩阵A中的最大值\n",
    "np.max(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求矩阵A中的最小值\n",
    "np.min(A)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 向量或者矩阵中的所有元素求平均数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11],\n",
       "       [12, 13, 14, 15]])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7.5"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.2"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v = np.array([1, 1, 2, 2, 10])\n",
    "np.mean(v)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 向量或者矩阵中的所有元素求积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11],\n",
       "       [12, 13, 14, 15]])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.prod(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2004189184"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.prod(A + 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 向量或者矩阵所有元素求中位数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11],\n",
       "       [12, 13, 14, 15]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7.5"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.median(A)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 按照百分比的方式查看向量或者矩阵中指定位置上元素的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9999973078296183"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看最大值\n",
    "np.percentile(big_array, q = 100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.835689500990668e-07\n",
      "0.24956973548846412\n",
      "0.49878257632319556\n",
      "0.7499890331739016\n",
      "0.9999973078296183\n"
     ]
    }
   ],
   "source": [
    "# 0%：最小值\n",
    "# 50%：中位数\n",
    "# 100%：最大值\n",
    "# 25%：介于最小值和中位数之间的中位数\n",
    "# 75%：介于中位数和最大值之间的中位数\n",
    "for percent in [0, 25, 50, 75, 100]:\n",
    "    print(np.percentile(big_array, q= percent))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 向量或者矩阵中的所有元素求方差和标准差"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.08339672583520141"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方差\n",
    "np.var(big_array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.28878491275549945"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 标准差\n",
    "np.std(big_array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 生成一组服从正态分布的随机数\n",
    "A = np.random.normal(0, 1, size = 1000000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.0030288144426415765"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 均值趋近于0\n",
    "np.mean(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0013928408722736"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 标准差趋近于1\n",
    "np.std(A)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 矩阵的行或者列的求和，求最大值，最小值等操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11],\n",
       "       [12, 13, 14, 15]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([24, 28, 32, 36])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 沿着行的方向，求每一列的和\n",
    "np.sum(A, axis = 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 6, 22, 38, 54])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 沿着列的方向，求每一行的和\n",
    "np.sum(A, axis = 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([6., 7., 8., 9.])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 沿着行的方向，求每一列的平均数\n",
    "np.mean(A, axis = 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([12, 13, 14, 15])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 沿着行的方向，求每一列的最大值\n",
    "np.max(A, axis = 0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "其他聚合操作也类似，只需要指定axis属性，即可求矩阵行或者列的相关值"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "444.667px",
    "left": "636.875px",
    "top": "495.333px",
    "width": "241.448px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
